package com.yjd.pub.tag.service;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.system.dto.OperParamDto;
import com.yjd.comm.tag.model.TagInfoDto;
import com.yjd.comm.tag.model.TagModel;
import com.yjd.comm.tag.model.TagRelevanceModel;
import com.yjd.comm.tag.service.ITagRelevanceService;
import com.yjd.comm.tag.service.ITagService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.tag.dao.ITagDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>description</b>：标签业务实现<br>
 * <b>time</b>：2017-06-27 08:58:17 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("tagService")
public class TagServiceImpl extends BaseServiceImpl implements ITagService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public TagModel insert(TagModel model) throws Exception {
		return this.tagDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(TagModel model) throws Exception {
		return this.tagDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param tag_id
	 * @return
	 * @throws Exception
	 */
	public int delete(long tag_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("tag_id", tag_id);
		return this.tagDao.deleteModel(paramMap, true);
	}

	/**
	 * 查询某个对象的标签名称
	 *
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<TagInfoDto> getTagNameList(Map<String, Object> param) throws Exception {
		return this.tagDao.getTagNameList(param);
	}

	/**
	 * 新增一个标签
	 *
	 * @param model
	 * @param operParamDto
	 * @throws Exception
	 */
	@Override
	public TagModel insert(TagModel model, OperParamDto operParamDto) throws Exception {
		this.adminLog(operParamDto, "添加标签[" + FrameUtil.json(operParamDto)
				+ "]", FrameUtil.getTime(null));
		return this.tagDao.insertModel(model, true);
	}

	/**
	 * 修改一个标签信息
	 *
	 * @param model
	 * @param operParamDto
	 * @throws Exception
	 */
	@Override
	public int update(TagModel model, OperParamDto operParamDto) throws Exception {
		this.adminLog(operParamDto, "修改标签[" + FrameUtil.json(operParamDto)
				+ "]", FrameUtil.getTime(null));
		return this.tagDao.updateModel(model, true);
	}

	@Override
	public List<TagModel> queryTags(Long ref_id, ReferenceTypeEnum ref_type) throws Exception {

		List<TagModel> tagModelList = this.tagDao.getModelList(FrameUtil.newHashMap(), DbWREnums.READ.isWrite());
		if(null == ref_id){
			return tagModelList;
		}

		Map<Long,Long> tagIdMap = getTagListForMap(tagRelevanceService.getModelList(
				FrameUtil.newHashMap("ref_id",ref_id,"ref_type",ref_type.getCode()), DbWREnums.READ));

		for(TagModel tagModel:tagModelList){
			if(tagIdMap.containsKey(tagModel.getTag_id())){
				tagModel.getStrMap().put("isCheck",true);
			}
		}

		return tagModelList;
	}

	private Map<Long,Long> getTagListForMap(List<TagRelevanceModel> list)throws Exception{
		Map<Long,Long> tagIdMap = new HashMap<Long,Long>();
		for(TagRelevanceModel model:list){
			tagIdMap.put(model.getTag_id(),model.getRef_id());
		}
		return  tagIdMap;
	}

	@Resource
	private ITagRelevanceService tagRelevanceService;

	@Resource
	private ITagDao tagDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.tagDao;
	}

}
